#include <dbg.h>

using namespace std;

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        int slow = nums[0];
        int fast = nums[0];

        // 找到环中的相遇点
        do {
            slow = nums[slow];
            fast = nums[nums[fast]];
            dbg(slow, fast);
        } while (slow != fast);

        // 找到环的入口点
        slow = nums[0];
        while (slow != fast) {
            slow = nums[slow];
            fast = nums[fast];
            dbg(slow, fast);
        }

        return slow;
    }
};

int main() {
    Solution s;
    vector<int> nums{2, 5, 9, 6, 9, 3, 8, 9, 7, 1};
    dbg(s.findDuplicate(nums));
}